package app.softtech.freechat.database;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.database.Cursor;

public class DatabaseFriend {
	private int id;
	private int mUserId;
	private int mfriendId;
	private String mFriendName;
	private String mFriendAvatar;
	private String mFriendStatus;
	private String mFriendPhone;
	private String mLastMessage;
	private int mRequestId;
	
	// Constructor
	
	
	public DatabaseFriend(int id, int user_id, int friend_id,
			String mFriend_Name, String mFriend_Avatar, String mFriend_status,
			String mFriend_phone, String mLast_Message, int requestId) {
		super();
		this.id = id;
		this.mUserId = user_id;
		this.mfriendId = friend_id;
		this.mFriendName = mFriend_Name;
		this.mFriendAvatar = mFriend_Avatar;
		this.mFriendStatus = mFriend_status;
		this.mFriendPhone = mFriend_phone;
		this.mLastMessage = mLast_Message;
		this.mRequestId  = requestId;
	}
	
	public DatabaseFriend(int mUserId, int mfriendId, String mFriendName,
			String mFriendAvatar, String mFriendStatus, String mFriendPhone,
			String mLastMessage, int requestId) {
		this.mUserId = mUserId;
		this.mfriendId = mfriendId;
		this.mFriendName = mFriendName;
		this.mFriendAvatar = mFriendAvatar;
		this.mFriendStatus = mFriendStatus;
		this.mFriendPhone = mFriendPhone;
		this.mLastMessage = mLastMessage;
		this.mRequestId  = requestId;
	}

	public DatabaseFriend() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public DatabaseFriend(JSONObject obj){
		try {
			this.id = obj.getInt(ConstantsDB.ID);
			this.mUserId = obj.getInt(ConstantsDB.USER_ID);
			this.mfriendId = obj.getInt(ConstantsDB.FRIEND_ID);
			this.mFriendName = obj.getString(ConstantsDB.FRIEND_NAME);
			this.mFriendAvatar = obj.getString(ConstantsDB.FRIEND_AVATAR);
			this.mFriendPhone = obj.getString(ConstantsDB.FRIEND_PHONE);
			this.mFriendStatus = obj.getString(ConstantsDB.FRIEND_STATUS);
			this.mLastMessage = obj.getString(ConstantsDB.LAST_MESSAGE);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	// Getter and setter
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getUser_id() {
		return mUserId;
	}
	public void setUser_id(int user_id) {
		this.mUserId = user_id;
	}
	public int getFriend_id() {
		return mfriendId;
	}
	public void setFriend_id(int friend_id) {
		this.mfriendId = friend_id;
	}
	public String getFriend_Name() {
		return mFriendName;
	}
	public void setFriend_Name(String mFriend_Name) {
		this.mFriendName = mFriend_Name;
	}
	public String getFriend_Avatar() {
		return mFriendAvatar;
	}
	public void setFriend_Avatar(String mFriend_Avatar) {
		this.mFriendAvatar = mFriend_Avatar;
	}
	public String getFriend_Status() {
		return mFriendStatus;
	}
	public void setFriend_Status(String mFriend_status) {
		this.mFriendStatus = mFriend_status;
	}
	public String getFriend_Phone() {
		return mFriendPhone;
	}
	public void setFriend_Phone(String mFriend_phone) {
		this.mFriendPhone = mFriend_phone;
	}
	public String getLast_Message() {
		return mLastMessage;
	}
	public void setLast_Message(String mLast_Message) {
		this.mLastMessage = mLast_Message;
	}
	public int getRequestId() {
		return mRequestId;
	}
	public void setRequestId(int requestId) {
		this.mRequestId = requestId;
	}
	/**
	 *  Get all Value of DatabaseUser
	 * @return ContentValues
	 */
	public ContentValues getValuesFriend(){
		
		ContentValues values = new ContentValues();
		//values.put(ConstantsDB.ID, this.getId());
		values.put(ConstantsDB.USER_ID, this.getUser_id());
		values.put(ConstantsDB.FRIEND_ID, this.getFriend_id());
		values.put(ConstantsDB.FRIEND_NAME, this.getFriend_Name());
		values.put(ConstantsDB.FRIEND_AVATAR, this.getFriend_Avatar());
		values.put(ConstantsDB.FRIEND_STATUS, this.getFriend_Status());
		values.put(ConstantsDB.FRIEND_PHONE, this.getFriend_Phone());
		values.put(ConstantsDB.LAST_MESSAGE, this.getLast_Message());
		values.put(ConstantsDB.REQUEST_ID, this.getRequestId());
		return values;		
	}
	
	public String toJSONString(){
		JSONObject obj = new JSONObject();
		
		try {
			obj.put("id", this.getId());
			obj.put("user_id", this.getUser_id());
			obj.put("friend_id", this.getFriend_id());
			obj.put("friend_name", this.getFriend_Name());
			obj.put("friend_avatar", this.getFriend_Avatar());
			obj.put("friend_status", this.getFriend_Status());
			obj.put("friend_phone", this.getFriend_Phone());
			obj.put("last_message", this.getLast_Message());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return obj.toString();		
	}
	/**
	 * Set Values for DatabseFriend from Cursor after excute sql
	 * @param cursor
	 * @return DatabaseFriend
	 */
	public static DatabaseFriend setValuesFromCursor(Cursor cursor){
		DatabaseFriend friend = new DatabaseFriend();
		friend.setFriend_Avatar(cursor.getString(cursor.getColumnIndex(ConstantsDB.FRIEND_AVATAR)));
		friend.setFriend_id(cursor.getInt(cursor.getColumnIndex(ConstantsDB.FRIEND_ID)));
		friend.setFriend_Name(cursor.getString(cursor.getColumnIndex(ConstantsDB.FRIEND_NAME)));
		friend.setFriend_Phone(cursor.getString(cursor.getColumnIndex(ConstantsDB.FRIEND_PHONE)));
		friend.setFriend_Status(cursor.getString(cursor.getColumnIndex(ConstantsDB.FRIEND_STATUS)));
		friend.setId(cursor.getInt(cursor.getColumnIndex(ConstantsDB.ID)));
		friend.setUser_id(cursor.getInt(cursor.getColumnIndex(ConstantsDB.USER_ID)));
		friend.setLast_Message(cursor.getString(cursor.getColumnIndex(ConstantsDB.LAST_MESSAGE)));
		friend.setRequestId(cursor.getInt(cursor.getColumnIndex(ConstantsDB.REQUEST_ID)));
		return friend;
	}

	public void SetRequestIdZero(){
		this.mRequestId = 0;
	}
}
